<template>
    <el-dialog 
      id="mesworkingprocedureaddlist"
      title="工序选择"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="45%">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-table
        id="workingProcedureList"
        :data="dataList"
        height="250px"
        border
        stripe
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        :span-method="objectSpanMethod"
        style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="parentWorkingProcedureName"
            header-align="center"
            align="center"
            width="30"
            label="">
          </el-table-column>
          <el-table-column 
            prop="workingProcedureNum"
            header-align="center"
            align="center"
            v-if="workingProcedureNumFlag"
            label="工序编号">
          </el-table-column>
          <el-table-column
            prop="workingProcedureName"
            header-align="center"
            align="left"
            width="150px"
            label="工序名称">
          </el-table-column>
          <el-table-column
            prop="workingProcedureProperty"
            header-align="center"
            align="left"
            label="工序性质">
          </el-table-column>
          <el-table-column
            prop="equipmentCode"
            header-align="center"
            align="left"
            label="设备">
          </el-table-column>
          <el-table-column
            prop="enabledFlgName"
            header-align="center"
            width="80px"
            align="center"
            label="是否启用">
          </el-table-column>
        </el-table>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :disabled="dataListSelections.length <= 0">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dataList: [],
        dataForm: {
          id: ''
        },
        visible: false,
        workingProcedureNumFlag: false,
        dataListLoading: false,
        dataListSelections: [],
        pageIndex: 1,
        pageSize: -1,
        totalPage: 0,
        spanArr: [] // 合并单元格数组
      }
    },
    methods: {
      init () {
        this.visible = true
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/basic/mesworkingprocedure/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            this.spanArr = [] // 页码改变时清空，重新push
            this.rowMergeArr()  
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      //获取合并表格单元格数组方法
      rowMergeArr(){
        let contactDot = 0;
        this.dataList.forEach((item,index) => {
            item.index = index;
            if(index === 0){
                this.spanArr.push(1);
            }else{
              if(item.parentWorkingProcedureNum === this.dataList[index - 1].parentWorkingProcedureNum && item.parentWorkingProcedureNum !== ''){
                  this.spanArr[contactDot] +=1;
                  this.spanArr.push(0);
              }else{
                  this.spanArr.push(1);
                  contactDot = index;
              }
            }
        })
        console.log(this.spanArr)
      },
      //合并element表格单元格方法
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if(columnIndex === 1){
            const _row = this.spanArr[rowIndex]
            const _col = _row>0?1:0;
            return{
                  rowspan:_row,
                  colspan:_col                  
                }
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 提交所选工序
      dataFormSubmit (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.workingProcedureNum
        })
        var items = items ? [item] : this.dataListSelections.map(item => {
          return item
        })
        // 子组件给父组件传递选中工序ID
        this.$emit('workingProduceList', items)
        this.visible = false
      }
    }
  }
</script>

<style lang="scss">
// #mesworkingprocedureaddlist {
//   .el-table .cell {
//     height: 25px;
//   }
// }
</style>